package Board;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import Member.MemberItem;
import Member.MemberMapper;
import Proposal.ProposalItem;
import Reply.ReplyMapper;
import Reply2.Reply2Mapper;
import Subject.SubjectItem;
import Subject.SubjectMapper;
import Attachment.AttachmentItem;
import Attachment.AttachmentMapper;
import Content.ContentItem;
import Content.ContentMapper;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

@Controller
public class BoardController {
	@Autowired
	private ContentMapper contentMapper;
	
	@Autowired
	private SubjectMapper subjectMapper;
	
	@Autowired
	private ReplyMapper replyMapper;
	
	@Autowired
	private MemberMapper memberMapper;
	
	@Autowired
	private AttachmentMapper attachmentMapper;
	
	@Autowired
	private Reply2Mapper reply2Mapper;
	
	
	@RequestMapping("/common/notice.do")
	public @ResponseBody List<HashMap<String,Object>> list( String str, String temp,int board_id, Integer subject_id){
        return contentMapper.selectAll(str,temp,board_id,subject_id);
	}
	
	
	@RequestMapping("/common/content.do")
	public @ResponseBody HashMap<String,Object>  content(int content_id){
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("content", contentMapper.selectBynum(content_id));
		data.put("reply", replyMapper.selectByContent_id(content_id));
		data.put("subject", subjectMapper.selectContent_id(content_id));
		return data;
	}
	
	@RequestMapping("/common/reply2list.do")
	public @ResponseBody HashMap<String,Object> reply2list(int content_id,int team_id){
		HashMap<String,Object> list = new HashMap<String,Object>();
		
		list.put("reply2", reply2Mapper.selectByBoardId(content_id,team_id));
		list.put("files",attachmentMapper.selectByTeamId(content_id, team_id));
		
		return list;
	}
	
	@RequestMapping("/common/content2.do")
	public @ResponseBody HashMap<String,Object>  content2(int content_id,@RequestParam(value="team_id",required=false)Integer team_id){
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("content", contentMapper.selectBynum(content_id));
		if(team_id!=null){
			data.put("reply", reply2Mapper.selectByBoardId(content_id, team_id));
			data.put("files",attachmentMapper.selectByTeamId(content_id, team_id));
		}
		
		else{
			data.put("files",attachmentMapper.selectByContentId(content_id));
		}
		return data;
	}
	
	
	@RequestMapping("/common/content3.do")
	public @ResponseBody List<AttachmentItem>  content3(int content_id,int team_id){
		return attachmentMapper.selectByTeamId(content_id, team_id);
		
	}


	@RequestMapping("/common/notice_board.do")
	public @ResponseBody int  board(int content_id){
		return content_id;
	}
	
	@RequestMapping("/common/subject.do")
	public @ResponseBody List<SubjectItem> subject(){
		return subjectMapper.selectSubject(1);
	}
	
	@RequestMapping("/common/loadNotice.do")
	public @ResponseBody List<HashMap<String,Object>> notice(int board_id){
		return contentMapper.selectNotice(board_id);
	}
	
	//@RequestMapping("/common/write.do")
	//public @ResponseBody String wrtie(@RequestBody ContentItem content){
	//	contentMapper.insert(content);
	//	return "저장 되었습니다.";
	//	}
	
	@RequestMapping("/common/write.do")
	public String write(@RequestParam(value="file",required=false) List<MultipartFile> files, @RequestParam(value="subject",required=false) Integer subject,
							@RequestParam("title") String title,@RequestParam("content") String content,
							@RequestParam("notice_ck") int notice_ck,@RequestParam(value="team_id",required=false) Integer team_id,@RequestParam("board_id") Integer board_id,@RequestParam(value="page",required=false) String page) throws IOException{
		User user =(User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String page1="";
		if(board_id==1){
			page1="notice.html";
		}
		else if(board_id==2){
			page1="QnA.html";
		}
		else{
			page1=page;
		}
		
		ContentItem item = new ContentItem();
		item.setBoard_id(board_id);
		item.setContent(content);

		
		item.setNotice(notice_ck);
		
		if(subject==null){
			item.setSubject_id(null);
		}
		else{
			item.setSubject_id(subject);
		}
		item.setId(user.getUsername());
		
		item.setTitle(title);
		
		contentMapper.insert(item);

		if (files != null && files.isEmpty() == false) {
			AttachmentItem file = new AttachmentItem();
			for (MultipartFile multipartFile : files) {
				if(!multipartFile.getOriginalFilename().equals("")){
					file.setContent_id(item.getContent_id());
					file.setFile_name(multipartFile.getOriginalFilename());
					file.setFileSize(multipartFile.getSize());
					file.setFile(multipartFile.getBytes());
					file.setTeam_id(team_id);
					attachmentMapper.insert(file);
				}
			}
        }

		return "redirect:"+page1;
	}
	@RequestMapping("/common/hw.do")
	public String write(@RequestParam(value="file",required=false) List<MultipartFile> files, 
					@RequestParam("team_id") Integer team_id,
					@RequestParam("content_id") Integer content_id,
					@RequestParam("contest_reg_id") int contest_reg_id) throws IOException{

		if (files != null && files.isEmpty() == false) {
			AttachmentItem file = new AttachmentItem();
			for (MultipartFile multipartFile : files) {
				if(!multipartFile.getOriginalFilename().equals("")){
					file.setContent_id(content_id);
					file.setFile_name(multipartFile.getOriginalFilename());
					file.setFileSize(multipartFile.getSize());
					file.setFile(multipartFile.getBytes());
					file.setTeam_id(team_id);
					attachmentMapper.insert(file);
				}
			}
        }
		return "redirect:contest.html#?contest_reg_id="+contest_reg_id+"&content_id="+content_id;

	}

	
	
	@RequestMapping("/common/modify.do")
	public String modify(@RequestParam(value="file",required=false) List<MultipartFile> files, @RequestParam(value="subject",required=false) Integer subject,
							@RequestParam("title") String title,@RequestParam("content") String content,
						    @RequestParam("content_id") int content_id,
							@RequestParam(value="notice_ck" ,required=false) Integer notice_ck,@RequestParam(value="team_id",required=false) Integer team_id,@RequestParam("board_id") Integer board_id,
							@RequestParam(value="page" ,required=false) String page) throws IOException{
	
		ContentItem item = new ContentItem();
		item.setContent(content);
		item.setContent_id(content_id);
		if(notice_ck==null)
			item.setNotice(0);
		else
			item.setNotice(notice_ck);
		
		if(subject==null)
			item.setSubject_id(null);
		else
			item.setSubject_id(subject);
		
		String page1="";
		if(board_id==1){
			page1="notice.html";
		}
		else if(board_id==2){
			page1="QnA.html";
		}
		else{
			page1=page;
		}
		item.setTitle(title);
		
		contentMapper.updateById(item);
		
		if (files != null && files.isEmpty() == false) {
			AttachmentItem file = new AttachmentItem();
			for (MultipartFile multipartFile : files) {
				if(!multipartFile.getOriginalFilename().equals("")){
				file.setContent_id(item.getContent_id());
				file.setFile_name(multipartFile.getOriginalFilename());
				file.setFileSize(multipartFile.getSize());
				file.setFile(multipartFile.getBytes());
				file.setTeam_id(team_id);
				attachmentMapper.insert(file);
				}
			}
        }
		return "redirect:"+page1;
	}
	
	
	@RequestMapping("/common/fileList.do")
	public @ResponseBody List<AttachmentItem> fileList(int content_id){
		return attachmentMapper.selectByContentId(content_id);
	}
	
	@RequestMapping("/common/fileDelete.do")
	public @ResponseBody boolean fileDelete(int file_id){
		attachmentMapper.deleteById(file_id);
		
		return (attachmentMapper.selectById(file_id) ==null ? true:false);
	}
	
	
	

	@RequestMapping("/common/user.do")
	public @ResponseBody String user(){
		
		
		if(!(SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser"))){
			User user =(User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return user.getUsername();
		}
		return null;
	}
	
	@RequestMapping("/common/userAuthority.do")
	public @ResponseBody HashMap<String,Object> userAuthority(){
		if(!(SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser"))){
			User user =(User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return memberMapper.selectById(user.getUsername());
		}
		return null;
	}
	
	
	
	@RequestMapping("/common/replyWrite.do")
	public @ResponseBody void replyWrite(@RequestBody HashMap<String,Object> reply){
		replyMapper.insert(reply);
		contentMapper.upReplyCount(Integer.parseInt(( (String)reply.get("content_id") )));
	}
	
	@RequestMapping("/common/replyDelete.do")
	public @ResponseBody void replyDelete(int reply_id,int content_id ){
		replyMapper.deleteById(reply_id);
		contentMapper.downReplyCount(content_id);
	}
	
	@RequestMapping("/common/replyModify.do")
	public @ResponseBody void replyModify(@RequestBody HashMap<String,Object> reply){
		replyMapper.updateById(reply);
		
	}
	
	@RequestMapping("/common/reply2Write.do")
	public @ResponseBody HashMap<String,Object> reply2Write(@RequestBody HashMap<String,Object> reply){
		reply2Mapper.insert(reply);
		contentMapper.upReplyCount(Integer.parseInt((reply.get("content_id").toString())));
		return reply2Mapper.selectById(Integer.parseInt((reply.get("reply2_id").toString())));
	}
	
	@RequestMapping("/common/reply2Delete.do")
	public @ResponseBody void reply2Delete(int reply2_id,int content_id ){
		reply2Mapper.deleteById(reply2_id);
		contentMapper.downReplyCount(content_id);
	}
	
	@RequestMapping("/common/reply2Modify.do")
	public @ResponseBody HashMap<String,Object> reply2Modify(@RequestBody HashMap<String,Object> reply){
		reply2Mapper.updateById(reply);
		return reply2Mapper.selectById(Integer.parseInt(reply.get("reply2_id").toString()));
	}
	
	
	
	
	@RequestMapping("/common/mainNotice.do")
	public @ResponseBody List<ContentItem> mainNotice(){
		return contentMapper.selectByBoard_id(1);
	}
	
	@RequestMapping("/common/fileDownload.do")
	public String fileDownload(int id,HttpServletRequest request){
		AttachmentItem fileModel = attachmentMapper.selectById(id);
		request.setAttribute("fileModel",fileModel);
		return "forward:/FileDownloadServlet";
	}
	
	
	

}
